home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games: Greatest Hits 1996 / Amiga Games: Greatest Hits 1996.iso / archive / userbox / publicdomain / madhouse.lha / Madhouse / developer / C-Demo / demo.c < prev    next >
C/C++ Source or Header  |  1996-02-11  |  10KB  |  382 lines

  1. /*    demo.c
  2.     Shows how to use the Madhouse-interface with a C-like language.
  3.  
  4.    Instructions to compile the program:
  5.     - Perhaps your compiler doesn't like <pragma/...>, then use another
  6.       name for this directory.
  7.     - This program can be compiled without problems with MaxonC++ v3.
  8.       For other compilers, you might have to change something.
  9.       
  10.       Last revised February 11th, 1996 for Madhouse 2.0
  11. */
  12.  
  13. #include <dos/dos.h>
  14. #include <intuition/intuition.h>
  15. #include <intuition/screens.h>
  16. #include <pragma/graphics_lib.h>
  17. #include <pragma/intuition_lib.h>
  18. #include <pragma/exec_lib.h>
  19. #include <pragma/dos_lib.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23. #include <string.h>
  24.  
  25. #define ABNC 0x40
  26. #define ABC  0x80
  27.  
  28.  
  29. /* Prototypes */
  30. void write_stopblank();
  31. void read_prefs();
  32. void write_error( char *str );
  33.  
  34.  
  35. /* Variables */
  36. char text[40];
  37. short color, scroll;
  38. time_t start;
  39. short duration_sec=0;
  40. struct Library *IntuitionBase = NULL;
  41. struct Library *GfxBase = NULL;
  42. struct Screen *scr = NULL;
  43. struct Window *win = NULL;
  44. struct Message *msg = NULL;
  45. USHORT DemoPalette[] = {
  46.     0x0000, 0x0FFF
  47. };
  48. struct TextAttr topaz8 = {
  49.     "topaz.font",
  50.     8,
  51.     0,
  52.     0
  53. };
  54.  
  55. /***************************************************************************
  56. ***                          open_all()
  57. ***                  Opens all things we need.
  58. ***************************************************************************/
  59.  
  60. BOOL open_all()
  61. {
  62.     IntuitionBase = OpenLibrary("intuition.library",37);
  63.     if( !IntuitionBase ) return FALSE;
  64.     GfxBase = OpenLibrary("graphics.library",37);
  65.     if( !GfxBase ) return FALSE;
  66.  
  67.     /* Open a standard LowRes-screen. */
  68.     scr = OpenScreenTags(NULL,
  69.           SA_Depth,         1,
  70.           SA_Width,         320,
  71.         SA_Height,        256,
  72.           SA_Font,          &topaz8,
  73.           TAG_DONE);
  74.     if(!scr) return FALSE;
  75.     
  76.     /* Set the colors of the mouse-pointer to the background-colors. */
  77.     SetRGB4( &scr->ViewPort,17,0,0,0);
  78.     SetRGB4( &scr->ViewPort,18,0,0,0);
  79.     SetRGB4( &scr->ViewPort,19,0,0,0);
  80.     
  81.     win = OpenWindowTags(NULL,
  82.              WA_AutoAdjust,    TRUE,
  83.              WA_NoCareRefresh, TRUE,
  84.              WA_CustomScreen,  scr,
  85.              WA_Flags,         WFLG_RMBTRAP,
  86.              WA_Borderless,    TRUE,
  87.              WA_Activate,      TRUE,
  88.              WA_IDCMP,             IDCMP_MOUSEBUTTONS+IDCMP_RAWKEY,
  89.              TAG_DONE );
  90.     if( !win ) return FALSE;
  91.     
  92.     LoadRGB4( &scr->ViewPort, DemoPalette, 2 );
  93.     
  94.     return TRUE;
  95. }
  96.  
  97. /***************************************************************************
  98. ***                          close_all()
  99. ***                 Closes all things we needed.
  100. ***************************************************************************/
  101.  
  102. void close_all()
  103. {
  104.     /* This brings the screen behind all others and should avoid some
  105.        stupid effects when closing screen with 68000-Amigas. */
  106.     if( scr )
  107.         ScreenToBack( scr );
  108.     
  109.     /* If something went wrong, we should tell this Madhouse. But mustn't
  110.        write more then one lines in the errors-file. So we use this nice
  111.         if-construct: */
  112.     if( !IntuitionBase ) write_error( "No intuition.lib V37!" );
  113.     else {
  114.         if( !GfxBase ) write_error( "No graphics.lib V37!" );
  115.         else {
  116.             if( !scr ) write_error( "Out of memory: Couldn't open screen.");
  117.             else {
  118.                 if( !win ) write_error( "Out of memory: Couldn't open window.");
  119.             }
  120.         }
  121.     }
  122.     
  123.     /* Now we close all what we could open. */
  124.     if( win ) CloseWindow( win );
  125.     if( scr ) CloseScreen( scr );
  126.     if( GfxBase ) CloseLibrary( GfxBase );
  127.     if( IntuitionBase ) CloseLibrary( IntuitionBase );
  128. }
  129.  
  130. /***************************************************************************
  131. ***                          main()
  132. ***                Here the real blanker begins.
  133. ***************************************************************************/
  134.  
  135. void main()
  136. {
  137.     BOOL quit = FALSE;
  138.     BOOL user_quit = FALSE;
  139.     short ypos = 20;
  140.     short direction = 1;
  141.     short oldy;
  142.     
  143.     /* First read the settings for our blanker and the duration-value.*/
  144.     read_prefs();
  145.     
  146.     /* Now we start our stop-watch. */
  147.     start = time(NULL);
  148.     
  149.     /* Open screen and window. */
  150.     if(open_all()) {
  151.         
  152.         /* Color-parameter: set the right color. */
  153.         switch( color ) {
  154.             case 0:
  155.                 SetRGB4( &scr->ViewPort, 1, 15,0,0 );
  156.                 break;
  157.             case 1:
  158.                 SetRGB4( &scr->ViewPort, 1, 15,15,0 );
  159.                 break;
  160.             case 2:
  161.                 SetRGB4( &scr->ViewPort, 1, 0,0,15 );
  162.         }
  163.         
  164.         /* Text-parameter: write the text. */
  165.         SetAPen( win->RPort, 1 );
  166.         Move( win->RPort, 5, ypos );
  167.         Text( win->RPort, text, ( strlen(text) > 38 ? 38 : strlen(text)) );
  168.         
  169.         /* Main-Loop. */
  170.         while( !quit ) {
  171.             
  172.             /* If the user selected Scroll, scroll the text. */
  173.             if( scroll ) {
  174.                 oldy = ypos;
  175.                 ypos += direction;
  176.                 if( (ypos > 220)  ||  (ypos < 20) ) direction *= -1;
  177.                 
  178.                 ClipBlit(win->RPort, 5, oldy-10, win->RPort, 5, ypos-10, 310, 20, ABNC | ABC );
  179.                 /* Minterms ABNC | ABC -> copy the rectangle without modifications. */
  180.             }
  181.             
  182.             /* Mousbutton or key? */
  183.             if( msg = GetMsg(win->UserPort) ) {
  184.                 quit = TRUE;
  185.                 user_quit = TRUE;
  186.                 ReplyMsg( msg );
  187.             }
  188.             
  189.             /* When 'Change Blanker' is activated ( duration_sec is not 0 )
  190.                AND we blanked (one second) longer as we are allowed to
  191.                 ( difftime(time(NULL),start) > time_counter ) then quit. */
  192.             if( duration_sec && difftime(time(NULL),start) > duration_sec )
  193.                 quit = TRUE;
  194.             
  195.             
  196.             WaitTOF();
  197.         }
  198.         
  199.     }
  200.     
  201.     /* Close our stuff and -perhaps- write the error. */
  202.     close_all();
  203.     
  204.     /* If the user stopped our Blanker, we have to inform Madhouse about
  205.        this, otherwise it would perhaps (if 'Change Blanker' is activated)
  206.        start a new blanker. */
  207.     
  208.     if( user_quit )
  209.         write_stopblank();
  210.     
  211.     /* And finished! */
  212. }
  213.  
  214.  
  215. /***************************************************************************
  216. ***                                                                      ***
  217. ***              Functions for the communication with Madhouse           ***
  218. ***                                                                      ***
  219. ***************************************************************************/
  220.  
  221. /* Some global variables: */
  222.  
  223. char bu[1500];        /* The buffer bu[] includes the whole prefs-file. */
  224. short bu_seek = 0;    /* bu_seek is the read-offset of bu[]. We need this
  225.                          variable to see where we have to read the next
  226.                          parameter. */
  227.  
  228. /* First some useful functions: */
  229.  
  230. /***************************************************************************
  231. ***                          read_next_digit()
  232. ***              Reads the next parameter out of our prefs-file.
  233. ***                   The next parameter must be a number.
  234. ***************************************************************************/
  235.  
  236. long read_next_digit()
  237. {
  238.     short number_cnt = 0;
  239.     char number[50];
  240.     
  241.     while( bu[bu_seek] != (char) 0x0A ) {
  242.         number[number_cnt] = bu[bu_seek];
  243.         bu_seek++;
  244.         number_cnt++;
  245.     }
  246.     number[number_cnt] = (char) 0x00;
  247.  
  248.     /*  Overjump the (0x0A)*/
  249.     bu_seek+=1;
  250.     
  251.     return strtol( number, 0, 10 );
  252. }
  253.  
  254. /***************************************************************************
  255. ***                          read_next_string()
  256. ***              Reads the next parameter out of our prefs-file.
  257. ***                   The next parameter must be a string.
  258. ***************************************************************************/
  259.  
  260. void read_next_string( char *string, short maxlen )
  261. {
  262.     short text_cnt = 0;
  263.     
  264.     /* A Madhouse-Text begins begins with a "$", so we jump over the first
  265.        char. */
  266.     bu_seek++;
  267.     
  268.     
  269.     while( (bu[bu_seek] != (char) 0x0A)  &&  (text_cnt < maxlen)  ) {
  270.         string[text_cnt] = bu[bu_seek];
  271.         bu_seek++;
  272.         text_cnt++;
  273.     }
  274.     
  275.     string[text_cnt] = (char) 0x00;
  276.     /* Now we have the text in string[]. */
  277.     
  278.     
  279.     /*
  280.      *  Jump over the 0x0A
  281.      */
  282.     
  283.     bu_seek+=1;
  284. }
  285.  
  286.  
  287.  
  288.  
  289. /***************************************************************************
  290. ***                             read_prefs()
  291. ***                        Reads the preferences.
  292. ***************************************************************************/
  293.  
  294. void read_prefs()
  295. {
  296.     short text_cnt = 0;
  297.     short number_cnt = 0;
  298.     BPTR f = NULL;
  299.     
  300.     f = Open( "RAM:Madhouse_Storage/prefs", MODE_OLDFILE );
  301.     if (f) {
  302.         Read( f, bu, sizeof( bu ) );
  303.         
  304.         /* Now, bu[] has a contents like this:
  305.            "$Hello, this is a text(0x0A)2(0x0A)0(0x0A)
  306.             5(0x0A)RAM:Madhouse_Storage(0x0A)".
  307.            (0x0A) is between every line.
  308.            Hello,... is our first parameter, 2 our second, 0 our third,
  309.             5 the duration in minutes and RAM:.. a path to our directory,
  310.             if we need data from it.
  311.             All Madhouse-strings begin with a "$".
  312.            We will put the first parameter in text[], the second in color,
  313.             the duration in duration_sec and the path is not needed. */
  314.         
  315.         /*
  316.          *   First parameter (Text)
  317.          */
  318.          
  319.         read_next_string( text, 40 );
  320.         
  321.         /*
  322.          *  Second parameter (Color)
  323.          */
  324.         
  325.         color = read_next_digit();
  326.         
  327.         /*
  328.          *  Third parameter (Scroll)
  329.          */
  330.         
  331.         scroll = read_next_digit();
  332.         
  333.         /*
  334.          *  Duration-Parameter.
  335.          */
  336.         
  337.         duration_sec = read_next_digit() *  60; /*Minutes -> Seconds*/
  338.         
  339.         Close( f );
  340.     } else {
  341.         /* The Blanker was not started by Madhouse. Quit here.*/
  342.         exit(0);
  343.     }
  344. }        
  345.  
  346. /***************************************************************************
  347. ***                           write_stopblank()
  348. ***           Must be used if the user aborted the blanker himself.
  349. ***************************************************************************/
  350.  
  351. void write_stopblank()
  352. {
  353.     /* Just create a new empty file to inform Madhouse that the user has
  354.        aborted the blanker. */
  355.     
  356.     BPTR f;
  357.     f = Open( "RAM:Madhouse_Storage/stopblank", MODE_NEWFILE );
  358.     if( f )
  359.         Close( f );
  360. }
  361.  
  362. /***************************************************************************
  363. ***                           write_error()
  364. ***            Tells Madhouse that an error occurred.
  365. ***************************************************************************/
  366.  
  367. void write_error( char *str )
  368. {
  369.     BPTR f;
  370.     
  371.     f = Open( "RAM:Madhouse_Storage/errors", MODE_READWRITE );
  372.     if( f ) {
  373.         Seek( f, 0, OFFSET_END );
  374.         Write( f, str, strlen(str) );
  375.         Close( f );
  376.     } 
  377. }
  378.  
  379.  
  380.  
  381.  
  382.